/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.xpath.model;

import javax.jcr.PropertyType;
import offset.nodes.Constants;
import offset.nodes.client.model.TreeNodeUserObject;

/**
 *
 * @author Walter Lütgenau
 */
public class ExpressionNode implements TreeNodeUserObject {

    public static final String TYPE_EXPRESSION = "expression";
    String childName = "";
    String propertyName;
    int propertyType = PropertyType.UNDEFINED;
    Operator operator;
    String value;

    public ExpressionNode(String propertyName, Operator operator, String value, int propertyType) {
        this.propertyName = propertyName;
        this.operator = operator;
        this.value = value;
        this.propertyType = propertyType;
    }

    public static ExpressionNode createEmpyExpression() {
        return new ExpressionNode(null, Operator.none, null, PropertyType.UNDEFINED);
    }

    public boolean isEmpty() {
        return propertyName == null;
    }

    public String getType() {
        return TYPE_EXPRESSION;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public int getPropertyType() {
        return propertyType;
    }

    public void setPropertyType(int propertyType) {
        this.propertyType = propertyType;
    }

    protected String quoteValue(String value) {
        if (propertyType != PropertyType.LONG && propertyType != PropertyType.DOUBLE)
            return "'" + value + "'";

        return value;
    }

    public String getChildName() {
        return childName;
    }

    public void setChildName(String childName) {
        this.childName = childName;
    }

    public String toString() {
        if (isEmpty())
            return "";

        XPathOperator xop = XPathOperators.get(operator);

        String child = "";
        if (childName != null && childName.length() > 0)
            child = childName + Constants.PATH_SEPARATOR;
        if (xop != null)
            if (xop.getType() == XPathOperatorType.operator)
                return child + "@" + propertyName + " " + xop.getXPathOperator() + " " + quoteValue(value);
            else if (xop.getType() == XPathOperatorType.function)
                return xop.getXPathOperator() + "(" + child + "@" + propertyName + ",'" + value + "')";
        return "";
    }
}
